<template>
  <!-- pages/promotion/promotion.wxml -->
  <view class="promotion-container">
    <view class="banner">
      <!-- 使用 :src 绑定而不是 {{ }} -->
      <image :src="bannerImageUrl" mode="aspectFill"></image>
    </view>
    
    <!-- 使用 v-if 代替 wx:if -->
    <view v-if="loading" class="loading-container">
      <text class="loading-text">加载中...</text>
    </view>
    
    <!-- 使用 v-else 代替 wx:else -->
    <view v-else class="product-list">
      <!-- 使用 v-for 和 :key -->
      <view class="product" v-for="(product, index) in products" :key="product.id">
        <view class="product-img">
          <!-- 使用 :src 绑定 -->
          <image :src="product.imgUrl" mode="aspectFill"></image>
        </view>
        <view class="product-info">
          <view class="product-name">{{ product.name }}</view>
          <view class="product-desc">{{ product.desc }}</view>
          <view class="product-price">
            <text>卷后￥{{ product.price }}</text>
            <!-- 使用 @click 绑定事件，直接传递参数 -->
            <button class="buy-button" @click="handleBuy(product.id)">
              <text class="buy-text">立即抢购</text>
            </button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>  

<script>
export default {
  data() {
    return {
      loading: true,
      products: [
        {
          id: 1,
          name: '华亚（HUAYA）哑铃男士杠铃套装',
          desc: '家用健身器材，可调节重量，适合初学者',
          // 使用绝对路径
          imgUrl: '/static/images/discount/1.jpg',
          price: 80.8
        },
        {
          id: 2,
          name: '启拓者【拉断包赔】拉力绳弹力绳门',
          desc: '多功能拉力器，锻炼全身肌肉，便携易用',
          imgUrl: '/static/images/discount/2.jpg',
          price: 28.00
        },
        {
          id: 3,
          name: 'Keep 俯卧撑训练板 便携俯卧撑支架',
          desc: '专业健身辅助器材，保护手腕，提升训练效果',
          imgUrl: '/static/images/discount/3.jpg',
          price: 70.00
        }
      ],
      bannerImageUrl: '/static/images/discount/4.jpg'
    };
  },
  mounted() {
    // 模拟数据加载
    setTimeout(() => {
      this.loading = false;
    }, 800);
  },
  methods: {
    handleBuy(productId) {
      console.log('购买商品ID:', productId);
      uni.showModal({
        title: '提示',
        content: `即将前往商店`,
        showCancel: false,
		 success: (res) => { // 添加 success 回调
		          if (res.confirm) {
		            console.log('用户点击了确定按钮');
		            
		            // 在确认后执行跳转
		            // 根据目标页面类型选择合适的跳转方式
		            
		            // 选项1: 跳转到标签页（如果是 tabBar 页面）
		            uni.switchTab({
		              url: '/pages/store/store'
		            });
		            
		            // 选项2: 跳转到普通页面（如果不是 tabBar 页面）
		            // uni.navigateTo({
		            //   url: '/pages/other/other'
		            // });
		          }
		        }
		        
      });
	  // uni.switchTab({
	  // 	url:"/pages/store/store"
	  // });;
	  
	  
    }
  }
};
</script>

<style>
/* pages/promotion/promotion.wxss */
.promotion-container {
  padding: 20rpx;
  background-color: #f8f8f8;
  min-height: 100vh;
}

.banner {
  height: 400rpx;
  width: 100%;
  margin-bottom: 30rpx;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.1);
}

.banner image {
  width: 100%;
  height: 400rpx;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300rpx;
}

.loading-text {
  font-size: 28rpx;
  color: #666;
}

.product-list {
  display: flex;
  flex-direction: column;
}

.product {
  display: flex;
  margin-bottom: 30rpx;
  background-color: #fff;
  padding: 20rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
}

.product-img {
  width: 220rpx;
  height: 220rpx;
  margin-right: 20rpx;
  border-radius: 8rpx;
  overflow: hidden;
}

.product-img image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-name {
  font-size: 28rpx;
  font-weight: 500;
  line-height: 36rpx;
  margin-bottom: 10rpx;
  color: #333;
}

.product-desc {
  font-size: 24rpx;
  line-height: 30rpx;
  color: #666;
  height: 60rpx;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-bottom: 10rpx;
}

.product-price {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.product-price text {
  font-size: 32rpx;
  color: #FF4500;
  font-weight: bold;
}

.buy-button {
  background: linear-gradient(to right, #FF4500, #FF6347);
  color: white;
  border: none;
  border-radius: 30rpx;
  padding: 14rpx 30rpx;
  font-size: 26rpx;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 10rpx rgba(255,69,0,0.2);
  transition: all 0.3s;
}

.buy-button:active {
  transform: scale(0.96);
  box-shadow: 0 2rpx 6rpx rgba(255,69,0,0.3);
}

.buy-text {
  margin-right: 8rpx;
  color: white!important ;
}  

</style>